package jk.spider.util.config.properties;

import jk.spider.util.ParserUtil;
import jk.spider.util.config.ConfigConstants;
import jk.spider.util.config.MappedPropertySet;
import jk.spider.util.config.PropertySet;
import jk.spider.util.config.SpiderConfiguration;

import org.apache.log4j.Logger;

public class PropertiesConfiguration implements SpiderConfiguration {
	private static final Logger log = Logger.getLogger(PropertiesConfiguration.class);
	protected PropertySet spiderProperties;
	protected PropertySet siteProps;
	protected PropertySet threadsProps;
	protected PropertySet throttleProps;
	protected PropertySet configProps;
	protected PropertySet pluginProps;
	protected PropertySet seedProps;
	protected PropertySet storageProps;
	protected PropertySet urlProps;
	protected PropertySet extractProps;
	protected PropertySet proxyProps;
	protected PropertySet msProps;
	
	public PropertiesConfiguration (String site, String path) {
		this.init(site, "", "", "");
	}
	

	public PropertiesConfiguration(String site, String file, String path) {
		this.init(site, file, "", "");
	}
	
	public PropertiesConfiguration(String site, String file, String type, String path) {
		this.init(site, file, type, path);
	}
	
	private void init(String site, String file, String type, String path) {
		String jspiderHome = path;
		if(!ParserUtil.isStringNull(file))
			file = file + "/";
		String spierConfiguration = "config/spider/" + file;
		if(!ParserUtil.isStringNull(type))
			type = "_" + type;
		String fileName = "spider_" + site + type + ".properties";
		
		
		log.info("JK_SpiderHome [ " + jspiderHome +" ]");
        if ( jspiderHome == null || "".equals(jspiderHome.trim()) || "null".equals(jspiderHome.trim())) {
        	jspiderHome = ".";
        }
        spiderProperties = PropertiesFilePropertySet.getInstance(jspiderHome, spierConfiguration, fileName);
	}
	
	public PropertySet getSpiderConfiguration() {
		return spiderProperties;
	}


	public synchronized PropertySet getSiteConfiguration() {
		if(siteProps == null)
			siteProps = new MappedPropertySet ( ConfigConstants.CONFIG_SITE, this.getSpiderConfiguration() );
		return siteProps;
	}


	public synchronized PropertySet getThreadsConfiguration() {
		if(threadsProps == null)
			threadsProps = new MappedPropertySet ( ConfigConstants.CONFIG_THREAD, this.getSpiderConfiguration() );
		return threadsProps;
	}


	public synchronized PropertySet getThrottleConfiguration() {
		if(throttleProps == null)
			throttleProps = new MappedPropertySet ( ConfigConstants.CONFIG_THROTTLE, this.getSpiderConfiguration() );
		return throttleProps;
	}


	public synchronized PropertySet getConfigConfiguration() {
		if(configProps == null)
			configProps = new MappedPropertySet ( ConfigConstants.CONFIG_CONFIG, this.getSpiderConfiguration() );
		return configProps;
	}


	public synchronized PropertySet getPluginConfiguration() {
		if(pluginProps == null)
			pluginProps = new MappedPropertySet ( ConfigConstants.CONFIG_PLUGIN, this.getSpiderConfiguration() ); 
		return pluginProps;
	}


	public synchronized PropertySet getSeedConfiguration() {
		if(seedProps == null)
			seedProps = new MappedPropertySet ( ConfigConstants.CONFIG_SEED, this.getSpiderConfiguration() );
		return seedProps;
	}


	public synchronized PropertySet getStorageConfiguration() {
		if(storageProps == null)
			storageProps = new MappedPropertySet ( ConfigConstants.CONFIG_STORAGE, this.getSpiderConfiguration() );
		return storageProps;
	}


	public synchronized PropertySet getUrlConfiguration() {
		if(urlProps == null)
			urlProps = new MappedPropertySet ( ConfigConstants.CONFIG_URL, this.getSpiderConfiguration() );
		return urlProps;
	}


	public synchronized PropertySet getExtractConfiguration() {
		if(extractProps == null)
			extractProps = new MappedPropertySet ( ConfigConstants.CONFIG_EXTRACT, this.getSpiderConfiguration() );
		return extractProps;
	}


	public synchronized PropertySet getProxyConfiguration() {
		if(proxyProps == null)
			proxyProps = new MappedPropertySet ( ConfigConstants.CONFIG_PROXY, this.getSpiderConfiguration() );
		return proxyProps;
	}
	
	public synchronized PropertySet getMssqlConfiguration() {
		if(msProps == null)
			msProps = new MappedPropertySet ( ConfigConstants.CONFIG_MSSQL, this.getSpiderConfiguration() );
		return msProps;
	}

}
